package com.example.demo.controller.admin;

import com.example.demo.entity.Article;
import com.example.demo.service.admin.ArticleAdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/admin/article")
@Controller
public class ArticleAdminController {

    @Autowired
    private ArticleAdminService articleAdminService;
    /**
     * 添加文章 返回添加文章界面
     * @return
     */
    @RequestMapping(value = "/addArticle", method = RequestMethod.GET)
    public String toArticle() {
        return "/admin_addArticle";
    }

    /**
     * 添加文章
     * @param article
     * @return
     */
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public Boolean addArticle(@RequestBody Article article) {
        if (article == null) {
            return false;
        }
        return articleAdminService.addArticle(article);
    }

    /**
     * 根据ID删除文章 重定向到selectArticle
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String toDelete(Integer id) {
        articleAdminService.deleteArticleById(id);
        return "redirect:selectArticle";
    }

    /**
     * 根据ID更新文章 返回查询文章界面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/updateArticle", method = RequestMethod.GET)
    public String toUpdateArticle(Integer id, Model model) {
        model.addAttribute("article", articleAdminService.selectArticleById(id));
        return "admin_updateArticle";
    }

    /**
     * 更改文章
     * @param article
     * @return
     */
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public Boolean updateArticle(@RequestBody Article article) {
        if (article == null) {
            return false;
        }
        return articleAdminService.addArticle(article);
    }

    /**
     * 查询所以文章 返回文章列界面
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectArticle", method = RequestMethod.GET)
    public String toSelectArticle(Model model) {
        List<Article> list = articleAdminService.selectAll();
        model.addAttribute("article",list );
        return "admin_articleLists";
    }

    /**
     * 查询所有文章
     *
     * @return List<Article>
     * @author yjq
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public List<Article> selectAllArticle() {
        return articleAdminService.selectAll();
    }

    /**
     * 根据文章名查询所有文章
     *
     * @return List<Article>
     * @author yjq
     */
    @RequestMapping(value = "/selectByArticleTitle", method = RequestMethod.GET)
    @ResponseBody
    public List<Article> selectByArticleTitle(String articleTitle) {
        return articleAdminService.selectByArticleTitle(articleTitle);
    }

    /**
     * 分页写法
     *
     * @param pageNo
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    public String getPage(Integer pageNo, Integer pageSize, Model model) {
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 3;
        }
        PageInfo<Article> pageInfo = articleAdminService.getPage(pageNo, pageSize);
        model.addAttribute("page", pageInfo);
        return "articleList";
    }
}
